package cn.wolfcode.p2p.website.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.wolfcode.p2p.base.anno.RequiredLogin;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.BidConst;
import cn.wolfcode.p2p.base.util.LoginInfoContext;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.service.IBidRequestService;

/**
 * 前台借款控制器
 */
@Controller
public class BidRequestController {
	@Autowired
	private IBidRequestService bidRequestService;
	@Autowired
	private IUserInfoService userInfoService;
	@Autowired
    private IAccountService accountService;

	@RequestMapping("/borrowInfo")
	@RequiredLogin
	public String toBorrowInfo(Model model){
		UserInfo userInfo = userInfoService.get(LoginInfoContext.getCurrentLoginInfo().getId());
		// 判断借款条件是否满足
        if (!userInfo.getBorrowble()){
            // 条件不满足重定向到我要借款页面
            return "redirect:/borrow";
        }
        // 判断是否存在借款申请
        if (userInfo.getHasBidRequestInProcess()){
            return "borrow_apply_result";
        }
        model.addAttribute("account", accountService.get(userInfo.getId()));
        model.addAttribute("minBidRequestAmount", BidConst.MIN_BIDREQUEST_AMOUNT);
        model.addAttribute("minBidAmount", BidConst.MIN_BID_AMOUNT);
        return "borrow_apply";
	}

    /**
     * 提交借款申请
     */

    @RequestMapping("/borrow_apply")
    public String applyBorrow(BidRequest vo){

        bidRequestService.applyBorrow(vo);

        return "redirect:/borrowInfo";
    }
}
